package com.ruoyi.common.utils;

import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.contant.CompanyConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class K100Utils {

    @Value("${KD100.key}")
    private String key;

    @Value("${KD100.customer}")
    private String customer;


    /**
     * 实时快递查询接口
     */
    public static void main(String args[]) throws Exception{

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(CompanyConstant.YT);
        queryTrackParam.setNum("YT6727873799913");
//        queryTrackParam.setCom("dfwl");
//        queryTrackParam.setNum("72122090851806");
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer("EC4C9E5DD8390F4EC00430FB096D185A");
        queryTrackReq.setSign(SignUtils.querySign(param ,"rQUtXLna6042","EC4C9E5DD8390F4EC00430FB096D185A"));

        IBaseClient baseClient = new QueryTrack();
        HttpResult result = baseClient.execute(queryTrackReq);
        System.out.println(result);
        String json = result.getBody();
        System.out.println(json);
    }


    /**
     * 实时快递查询接口
     * @param com 物流公司编码
     * @param num 单号
     * @return
     * @throws Exception
     */
    public String queryTrack(String com, String num) throws Exception{

        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(com);
        queryTrackParam.setNum(num);
        String param = new Gson().toJson(queryTrackParam);

        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));

        IBaseClient baseClient = new QueryTrack();
        HttpResult result = baseClient.execute(queryTrackReq);
        System.out.println(result);
        String json = result.getBody();
        System.out.println(json);
        return json;
    }


}
